﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NXT.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records.Obsolete
{
    #region Usings

    using System;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Text;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The NXT record.
    /// </summary>
    [Obsolete("Record no longer used. Provided for legacy purposes only.")]
    public class NXT : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NXT"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public NXT(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            var length = recordReader.ReadUInt16(-2);
            this.NextDomainName = recordReader.ReadDomainName();
            length -= (ushort)recordReader.Position;
            this.Bitmap = new byte[length];
            this.Bitmap = recordReader.ReadBytes(length);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Bitmap.
        /// </summary>
        public byte[] Bitmap { get; private set; }

        /// <summary>
        ///     Gets Next domain name.
        /// </summary>
        public string NextDomainName { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var sb = new StringBuilder();
            for (var bitNr = 1; bitNr < (this.Bitmap.Length * 8); bitNr++)
            {
                if (this.IsSet(bitNr))
                {
                    sb.Append(" " + (RecordType)bitNr);
                }
            }

            return string.Format(CultureInfo.InvariantCulture, "{0}{1}", this.NextDomainName, sb);
        }

        #endregion

        #region Methods

        /// <summary>
        /// Determines whether the specified bit nr is set.
        /// </summary>
        /// <param name="bitNr">
        /// The bit nr.
        /// </param>
        /// <returns>
        /// <c>true</c> if the specified bit nr is set; otherwise, <c>false</c>.
        /// </returns>
        private bool IsSet(int bitNr)
        {
            Contract.Requires(0 <= (bitNr / 8));
            Contract.Requires((bitNr / 8) < this.Bitmap.Length);

            var intByte = bitNr / 8;
            var intOffset = bitNr % 8;
            var b = this.Bitmap[intByte];
            var intTest = 1 << intOffset;
            if ((b & intTest) == 0)
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}